package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.base.DataSourceManager;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.BelongToSqlUtils;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminZTJDao;
import jzqb.dao.AdminZTWXDao;
import jzqb.po.ZTJPo;
import jzqb.po.ZTWXPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 专题文献数据层实现类
 */
@Repository
public class AdminZTWXDaoImpl implements AdminZTWXDao {
    @Autowired
    private DataContext dataContext;

    @Autowired
    private DataSourceManager kbaseDs;

    @Value("${filePath.fileTemPResource}")
    private String fileTemPResource;

    @Value("${filePath.ztwxFilePath}")
    private String ztwxFilePath;

    @Override
    public Boolean updateZTWX(ZTWXPo po) {
        boolean fileRes = true;
        String newDigitFileName = po.getNewDigitFileName();
        String digitFileName = po.getDigitFileName();
        String delete = po.getNeedDelete();
        String dbAbsPath,tempPath,before= "";
        //1. 把新文件从临时目录移动到数据库，并且删除原文件
        if (!"".equals(newDigitFileName) && newDigitFileName != null){
            if (!"".equals(digitFileName) && digitFileName != null){
                String filePath = FileUtil.calculateAbstractPath(digitFileName,ztwxFilePath);
                FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, filePath.substring(0,filePath.lastIndexOf("\\")));
//                FileUtil.deleteFolder(filePath.substring(0,filePath.lastIndexOf("\\")));
            }
            dbAbsPath = FileUtil.calculateAbstractPath(newDigitFileName,ztwxFilePath);
            tempPath = FileUtil.calculateAbstractPath(newDigitFileName,fileTemPResource);
            try {
                FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
//                FileuploadUtils.fileMove(tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            po.setDigitFileName(newDigitFileName);
        }
        //2. 如果有需要删除的文件则进行删除
        if (!"".equals(delete) && delete != null) {
            before = FileUtil.calculateAbstractPath(delete,ztwxFilePath);
//            FileUtil.deleteFolder(before.substring(0, before.lastIndexOf("\\")));
            FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, before.substring(0, before.lastIndexOf("\\")));
        }
        //3. 更新
        String recStr = FileuploadUtils.generRECfile("fieldsRemap_ztwx.json","edit", digitFileName,po.getSysID(),po);
        String tableName = SysTool.ZTWX_METADATA;
        boolean updateRes = dataContext.updateRecData("SYS_FLD_SYSID", tableName, recStr);
        return updateRes && fileRes;
    }

    @Override
    public Boolean insertZTWX(ZTWXPo po) {
        String digitFileName = po.getDigitFileName();
        Integer sysID = po.getSysID();
        //1 写入数据到数据库
        String recStr = FileuploadUtils.generRECfile("fieldsRemap_ztwx.json","add",digitFileName, sysID,po);
        String tableName = SysTool.ZTWX_METADATA;
        boolean insetRes = dataContext.insertRecData(tableName,recStr);
        boolean fileRes = true;
        String dbAbsPath,tempPath = "";
        //2 把文件从 临时目录 移动到 数据库
        if (!"".equals(digitFileName)){
            dbAbsPath = FileUtil.calculateAbstractPath(digitFileName,ztwxFilePath);
            tempPath = FileUtil.calculateAbstractPath(digitFileName,fileTemPResource);
            try {
//                fileRes = FileuploadUtils.fileMove(tempPath,dbAbsPath);
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return insetRes && fileRes;
    }

    @Override
    public Boolean deleteZTWX(String delSql) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTWXDao.deleteZTWX";
        return sqlSession.executeDelete(statementId, delSql);

    }

    @Override
    public List<ZTWXPo> queryZTWX(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTWXDao.queryZTWX";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public Long countZTWX(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTWXDao.countZTWX";
        return sqlSession.getCount(statementId, bo);
    }

    @Override
    public List<ZTWXPo> getMaxZTWXId(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTWXDao.getMaxZTWXId";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public List<ZTWXPo> queryNameAndId(ZTWXPo po) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTWXDao.queryNameAndId";
        return sqlSession.selectList(statementId, po, false);
    }


}
